#!/usr/bin/env python
"""
扫描整个目录树中最大的Python源代码文件。
搜索Python源代码库，利用pprint漂亮地显示结果。
"""

import os
import pprint
import sys

TRACE = False


def getLibPath():
    if sys.platform[:3] == 'win':
        return 'C:\\Python\\Lib'
    else:
        return '/home/alone/anaconda3/lib'  # 需手动修改


def getFileList(rootdir):
    pyfiles = []
    for curdir, subdir, curfile in os.walk(rootdir):
        if TRACE:
            print(curdir)
        for eachfile in curfile:
            if eachfile.endswith('.py'):
                if TRACE:
                    print(eachfile)
                path = os.path.join(curdir, eachfile)
                size = os.path.getsize(path)
                pyfiles.append((size, path))
    return pyfiles


def bigpyTree(rootdir):
    pyfiles = getFileList(rootdir)
    pyfiles.sort()

    return pyfiles


def test():
    libpath = getLibPath()
    allfiles = bigpyTree(libpath)
    pprint.pprint(allfiles[:3])
    pprint.pprint(allfiles[-3:])


if __name__ == '__main__':
    test()
